<?php
require_once('Page.php');
require_once('Datenbank/db.php');

class Report extends HTMLPage implements Page{
 
	private $link = '';
	private $totals = array();
	
	public function __construct() {
		$this->link = Db::getConnection();
		
		$action = isset($_GET['action']) ? $_GET['action'] : '';
		
		if($action == "Delete") {
			$this->deleteReport();
		}
		
		$this->getData();
	}
	
	private function deleteReport() {
		$id = isset($_GET['datum']) ? $_GET['datum'] : '';
	
		$deleteSQL = "Delete from report where datum = '$id'";
		mysql_query($deleteSQL);
	}
	
	private function getData(){
		$abfrage_data = "Select * from report order by datum ASC";
		$ergebnis_data = mysql_query($abfrage_data);
		
		$counter = 0;
		$datum = '';
		$totalKonten = 0;
		$totalLiquideMittel = 0;
		$totalGebundeneMittel = 0;
		while($row = mysql_fetch_assoc($ergebnis_data))
		{
			$abfrage_kategorie = "Select kategorie from produkt where id = '".$row['produktfsid']."'";
			
			$ergebnis_kategorie = mysql_query($abfrage_kategorie);
			
			$kategorie = mysql_result($ergebnis_kategorie, 0);
			
			if($datum == ''){
				$datum = $row['datum'];
			}
			if($datum != $row['datum']){
				$this->totals[$counter]['datum'] = $datum;
				$this->totals[$counter]['totalKonten'] = $totalKonten;
				$this->totals[$counter]['totalLiquideMittel'] = $totalLiquideMittel;
				$this->totals[$counter]['totalGebundeneMittel'] = $totalGebundeneMittel;
				
				$totalKonten = 0;
				$totalLiquideMittel = 0;
				$totalGebundeneMittel = 0;
			}	
			
			$datum = $row['datum'];
			$totalKonten = $totalKonten + ($kategorie == 1 ? $row['aktuellerwert'] : 0);
			$totalLiquideMittel = $totalLiquideMittel + ($kategorie == 2 ? $row['aktuellerwert'] : 0);
			$totalGebundeneMittel = $totalGebundeneMittel + ($kategorie == 3 ? $row['aktuellerwert'] : 0);
			
			$counter++; 
		}
		$this->totals[$counter]['datum'] = $datum;
		$this->totals[$counter]['totalKonten'] = $totalKonten;
		$this->totals[$counter]['totalLiquideMittel'] = $totalLiquideMittel;
		$this->totals[$counter]['totalGebundeneMittel'] = $totalGebundeneMittel;
	}
	
	public function getBackgroundColor($oldValue, $newValue){
		$oldValue = number_format($oldValue, 2, '', '');
		$newValue = number_format($newValue, 2, '', '');
		if($oldValue == 0 || $oldValue === $newValue){
			return 'white';
		}
		else if($oldValue > $newValue){
			return '#FF4D00';
		}
		else{
			return '#00E500';
		}
	}
	
	public function getHTML() {
		include('layout/report.tpl');
	}
}

?>